﻿using KwaiXiaoDian.Data;
using KwaiXiaoDian.Entity.Common;
using KwaiXiaoDian.Entity.DO;
using KwaiXiaoDian.Entity.VO.Product;
using KwaiXiaoDian.IDao;
using SSharing.Frameworks.NetCore.Common.Extends;
using SSharing.Frameworks.NetCore.Common.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace KwaiXiaoDian.Dao
{
    /// <summary>
    /// 商品dao
    /// </summary>
    public class ProductDao : BaseDao<TProduct>, IProductDao
    {
        private readonly KwaiXiaoDianDbContext _kwaiXiaoDianDbContext;

        public ProductDao(KwaiXiaoDianDbContext kwaiXiaoDianDbContext) : base(kwaiXiaoDianDbContext)
        {
            _kwaiXiaoDianDbContext = kwaiXiaoDianDbContext;
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public IPagingResult<TProduct> GetPagingProducts(GetPagingProductsRequest request)
        {
            IPagingResult<TProduct> pagingResult = new PagingResult<TProduct>();
            var query = from a in _kwaiXiaoDianDbContext.TProduct
                        join b in _kwaiXiaoDianDbContext.TGroupLeader on a.LeaderId equals b.LeaderId into c
                        from d in c.DefaultIfEmpty()
                        join e in _kwaiXiaoDianDbContext.TProductShop on a.ProductId equals e.ProductId into f
                        from g in f.DefaultIfEmpty()
                        where d.IsEnabled == Enums.IsEnabledType.Y.ToString()
                        select new TProduct
                        {
                            Id = a.Id,
                            CreatedTime = a.CreatedTime,
                            LeaderId = a.LeaderId,
                            LeaderName = d.Name,
                            ProductId = a.ProductId,
                            DistributeItemId = a.DistributeItemId,
                            Name = a.Name,
                            ImageUrl = a.ImageUrl,
                            Price = a.Price,
                            ProfitAmount = a.ProfitAmount,
                            CommissionRate = a.CommissionRate,
                            DeliveryTime = a.DeliveryTime,
                            PromoterNum = a.PromoterNum,
                            SoldCountThirtyDays = a.SoldCountThirtyDays,
                            Status = a.Status,
                            Channel = a.Channel,
                            ShopId = g.ShopId,
                            ShopName = g.ShopName,
                            HandleStatus= a.HandleStatus,
                            Remark= a.Remark
                        };

            //查询条件
            if (!request.LeaderId.IsNullOrEmpty())
            {
                var leaderId = request.LeaderId.Trim();
                query = query.Where(p => p.LeaderId == leaderId);
            }
            if (!request.LeaderName.IsNullOrEmpty())
            {
                var leaderName = request.LeaderName.Trim();
                query = query.Where(p => p.LeaderName.Contains(leaderName));
            }
            if (!request.ProductId.IsNullOrEmpty())
            {
                var productId = request.ProductId.Trim();
                query = query.Where(p => p.ProductId == productId);
            }
            if (!request.Name.IsNullOrEmpty())
            {
                var productName = request.Name.Trim();
                query = query.Where(p => p.Name.Contains(productName));
            }
            if (!request.CreatedTimeStart.IsNullOrEmpty() && !request.CreatedTimeEnd.IsNullOrEmpty())
            {
                var createdTimeStart = request.CreatedTimeStart.ToDateTime();
                var createdTimeEnd = request.CreatedTimeEnd.ToDateTime();
                query = query.Where(p => p.CreatedTime >= createdTimeStart && p.CreatedTime <= createdTimeEnd);
            }
            if (!request.Status.IsNullOrEmpty())
            {
                var status = (byte)request.Status.ToEnum<Enums.ProductStatusType>();
                query = query.Where(p => p.Status == status);
            }
            if (!request.ChannelId.IsNullOrEmpty())
            {
                var channelId = "," + request.ChannelId.Trim() + ",";
                query = query.Where(p => p.Channel.Contains(channelId));
            }
            if (!request.ShopId.IsNullOrEmpty())
            {
                var shopId = request.ShopId.Trim();
                query = query.Where(p => p.ShopId == shopId);
            }
            if (!request.ShopName.IsNullOrEmpty())
            {
                var shopName = request.ShopName.Trim();
                query = query.Where(p => p.ShopName.Contains(shopName));
            }

            //自定义排序
            if (!request.OrderField.IsNullOrEmpty())
            {
                var orderField = request.OrderField;
                var orderBy = request.OrderBy;
                if (orderField.ToLower() == "promoternum")
                {
                    if (orderBy == "desc")
                    {
                        query = query.OrderByDescending(p => p.PromoterNum);
                    }
                    else if (orderBy == "asc")
                    {
                        query = query.OrderBy(p => p.PromoterNum);
                    }
                    else
                    {
                        //默认按升序排序
                        query = query.OrderBy(p => p.PromoterNum);
                    }
                }
                if (orderField.ToLower() == "soldcountthirtydays")
                {
                    if (orderBy == "desc")
                    {
                        query = query.OrderByDescending(p => p.SoldCountThirtyDays);
                    }
                    else if (orderBy == "asc")
                    {
                        query = query.OrderBy(p => p.SoldCountThirtyDays);
                    }
                    else
                    {
                        //默认按升序排序
                        query = query.OrderBy(p => p.SoldCountThirtyDays);
                    }
                }
            }
            else
            {
                //默认按创建时间倒序
                query = query.OrderByDescending(p => p.CreatedTime);
            }

            //分页
            return query.PagingWithNoLock(request.Page, request.Limit);
        }

        /// <summary>
        /// 获取待同步商品详情的商品列表
        /// </summary>
        /// <returns></returns>
        public IList<TProduct> GetProductDetails()
        {
            //查询条件：商品店铺记录为空
            IList<TProduct> products = new List<TProduct>();

            var query = from a in _kwaiXiaoDianDbContext.TProduct
                        join b in _kwaiXiaoDianDbContext.TProductExpress on a.ProductId equals b.ProductId into c
                        from d in c.DefaultIfEmpty()
                        join e in _kwaiXiaoDianDbContext.TProductShop on a.ProductId equals e.ProductId into f
                        from g in f.DefaultIfEmpty()
                        join h in _kwaiXiaoDianDbContext.TGroupLeader on a.LeaderId equals h.LeaderId into i
                        from j in i.DefaultIfEmpty()
                        where j.IsEnabled == Enums.IsEnabledType.Y.ToString()
                        select new TProduct
                        {
                            Id = a.Id,
                            CreatedTime = a.CreatedTime,
                            LeaderId = j.LeaderId,
                            LeaderName = j.Name,
                            ProductId = a.ProductId,
                            DistributeItemId = a.DistributeItemId,
                            Name = a.Name,
                            ProductExpressId = d.Id,
                            ProductShopId = g.Id,
                        };

            query = query.Where(p => p.ProductShopId == null);

            //排序
            query = query.OrderByDescending(p => p.CreatedTime);

            //返回结果
            products = query.ToList();

            return products;
        }

        /// <summary>
        /// 获取待同步商品达人排行榜的商品列表
        /// </summary>
        /// <returns></returns>
        public IList<TProduct> GetProductRanks()
        {
            IList<TProduct> products = new List<TProduct>();

            var query = from a in _kwaiXiaoDianDbContext.TProduct
                        join b in _kwaiXiaoDianDbContext.TGroupLeader on a.LeaderId equals b.LeaderId into c
                        from d in c.DefaultIfEmpty()
                        where d.IsEnabled == Enums.IsEnabledType.Y.ToString()
                        select new TProduct
                        {
                            Id = a.Id,
                            CreatedTime = a.CreatedTime,
                            LeaderId = d.LeaderId,
                            LeaderName = d.Name,
                            ProductId = a.ProductId,
                            Name = a.Name
                        };

            //排序
            query = query.OrderByDescending(p => p.CreatedTime);

            //返回结果
            products = query.ToList();

            return products;
        }


    }
}
